package com.leetcode.math;

public class StringMultiply {
    public static void main(String[] args) {
        String num1 = "999";
        String num2 = "999";
        String res = multiply(num1, num2);
        System.out.println(res);
    }

    public static String multiply(String num1, String num2){
        if(num1.equals("0") || num2.equals("0")){
            return "0";
        }
        int m = num1.length(), n = num2.length();
        int[] res = new int[m + n];
        for(int i = m - 1; i >= 0; i--){
            int x = num1.charAt(i) - '0';
            for(int j = n - 1; j >= 0; j--){
                int y = num2.charAt(j) - '0';
                int sum = x * y + res[i + j + 1];
                res[i + j + 1] = sum % 10;
                res[i + j] += sum / 10;
                int a = res[i + j];
            }
        }
        StringBuilder result = new StringBuilder();
        for(int i = 0; i < res.length; i++){
            if(i == 0 && res[i] == 0){
                continue;
            }
            result.append(res[i]);
        }
        return result.toString();
    }
}
